﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;

namespace outREACH.App_Code
{
    public class FinDonation
    {
        public int finDId { get; set; }
        public string userName { get; set; }
        public string creditCardName { get; set; }
        public string creditCardNo { get; set; }
        public int CVV { get; set; }
        public int amount { get; set; }
        public string paymentType { get; set; }
        public string billingAdd { get; set; }
        public string country { get; set; }
        public string city { get; set; }
        public string email { get; set; }
        public string zipCode { get; set; }
        public int hp { get; set; }
        public int home { get; set; }
        public string section { get; set; }
        public string countryToDonate { get; set; }
        public string NGOs { get; set; }
        public DateTime dateTime { get; set; }
        public string category { get; set; }

        public static void newDonation(FinDonation f)
        {
            SqlConnection conn = null;
            int newDonationID = 0;

            try
            {
                conn = new SqlConnection();
                SqlCommand comm = new SqlCommand();
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["outreachdbConnectionString"].ConnectionString;
                conn.Open();
                comm.Connection = conn;
                comm.CommandText = "insert into FinancialDonation(amount, userName, paymentType, creditCardName,creditCardNo, CVV, country, city, billingAdd, zipCode, email, hp, home, section, countryToDonate, NGOs, dateTime, category) VALUES (@amount, @userName, @paymentType,@creditCardName, @creditCardNo, @cvv, @country, @city, @billingAdd, @zipCode, @email, @hp, @home, @section, @countryToDonate, @NGOs, @dateTime, @category)";
                comm.Parameters.AddWithValue("@userName", f.userName);
                comm.Parameters.AddWithValue("@creditCardName", f.creditCardName);
                comm.Parameters.AddWithValue("@paymentType", f.paymentType);
                comm.Parameters.AddWithValue("@creditCardNo", f.creditCardNo);
                comm.Parameters.AddWithValue("@cvv", f.CVV);
                comm.Parameters.AddWithValue("@country", f.country);
                comm.Parameters.AddWithValue("@city", f.city);
                comm.Parameters.AddWithValue("@billingAdd", f.billingAdd);
                comm.Parameters.AddWithValue("@zipCode", f.zipCode);
                comm.Parameters.AddWithValue("@email", f.email);
                comm.Parameters.AddWithValue("@hp", f.hp);
                comm.Parameters.AddWithValue("@home", f.home);
                comm.Parameters.AddWithValue("@section", f.section);
                comm.Parameters.AddWithValue("@countryToDonate", f.countryToDonate);
                comm.Parameters.AddWithValue("@NGOs", f.NGOs);
                comm.Parameters.AddWithValue("@dateTime", f.dateTime);
                comm.Parameters.AddWithValue("@category", f.category);
                comm.Parameters.AddWithValue("@amount", f.amount);
                int rowsAffected = comm.ExecuteNonQuery();
                comm.CommandText = "select @@IDENTITY as NewDonationID";
                SqlDataReader dr = comm.ExecuteReader();
                dr.Read();
                newDonationID = int.Parse(dr["NewDonationID"].ToString());
                dr.Close();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }

        public static List<FinDonation> getFinDonation()
        {
            SqlConnection conn = null;
            List<FinDonation> result = new List<FinDonation>();
            try
            {
                conn = new SqlConnection();
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["outreachdbConnectionString"].ConnectionString;
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "select * from FinancialDonation order by [dateTime] desc";
                SqlDataReader dr = comm.ExecuteReader();
                while (dr.Read())
                {
                    FinDonation fd = new FinDonation();
                    fd.amount = (int)dr["amount"];
                    fd.billingAdd = (string)dr["billingAdd"];
                    fd.category = (string)dr["category"];
                    fd.city = (string)dr["city"];
                    fd.country = (string)dr["country"];
                    fd.countryToDonate = (string)dr["countryToDonate"];
                    fd.creditCardName = (string)dr["creditCardName"];
                    fd.creditCardNo = (string)dr["creditCardNo"];
                    fd.CVV = (int)dr["cvv"];
                    fd.dateTime = (DateTime)dr["dateTime"];
                    fd.email = (string)dr["email"];
                    fd.finDId = (int)dr["FD_ID"];
                    fd.home = (int)dr["home"];
                    fd.hp = (int)dr["hp"];
                    fd.amount = (int)dr["amount"];
                    fd.NGOs = (string)dr["NGOs"];
                    fd.paymentType = (string)dr["paymentType"];
                    fd.section = (string)dr["section"];
                    fd.userName = (string)dr["userName"];
                    fd.zipCode = (string)dr["zipCode"];
                    result.Add(fd);
                }
            }
            catch (SqlException e)
            {
                throw e;
            }
            finally
            {
                conn.Close();
            }
            return result;
        }
    }
}